#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
void _MergerSort(int* a, int begin, int end,int* tmp)
{
	assert(a);
	if (begin >= end)
	{
		return;
	}

	int mid = (begin + end) / 2;
	int begin1 = begin, end1 = mid;
	int begin2 = mid + 1, end2 = end;

	int start = begin1;

	_MergerSort(a, begin1, end1,tmp);
	_MergerSort(a, begin2, end2,tmp);


	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[start++] = a[begin1++];
		}
		else
		{
			tmp[start++] = a[begin2++];
		}
	}

	while (begin1 <= end1)
	{
		tmp[start++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[start++] = a[begin2++];
	}

	memcpy(a + begin, tmp + begin, sizeof(int) * (end-begin + 1));
}

void MergerSort(int* a, int begin, int end)
{
	assert(a);

	int* tmp = (int*)malloc(sizeof(a));
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}
	_MergerSort(a, begin, end, tmp);
	//
	free(tmp);
}

void MyPrintf(int* a, int n)
{
	assert(a);
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

int main()
{
	int a[] = { 11,42,61,928,-303,-4,-6,7,-9,44 ,-54 };
	
	MergerSort(a, 0, sizeof(a) / sizeof(int) - 1);

	MyPrintf(a, sizeof(a) / sizeof(int));
	return 0;
}




